package com.example.demogooglemapsv2;

import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.telephony.gsm.SmsManager;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements
//OnMapClickListener, OnMapLongClickListener {
OnMarkerClickListener {
	private final LatLng LOCATION_BURNABY = new LatLng(50.97235, 18.21807);
	private GoogleMap map;
	 private LocationManager locationManager;
	 private static final long MIN_TIME = 400;
	 private static final float MIN_DISTANCE = 1000;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// com.google.android.maps.MapView.LayoutParams params=new
		// com.google.android.maps.MapView.LayoutParams(500,200, null, 0);
		// map.setLayoutParams(params);
		// map.invalidate();
		Button c = (Button) findViewById(R.id.button1);
		c.setWidth(90);
		c.setHeight(30);
		Button c1 = (Button) findViewById(R.id.button2);
		c1.setWidth(90);
		c1.setHeight(30);
		 map = ((MapFragment) getFragmentManager()
					.findFragmentById(R.id.map)).getMap();
		//map.setOnMapLongClickListener(this);
		 map.setPadding(0, 50, 0, 100);
		 map.setOnMarkerClickListener(this);
		// map.
		// map.addMarker(new
		// MarkerOptions().position(LOCATION_BURNABY).title("TUT"));

		// Get a handle to the Map Fragment
		// GoogleMap map = ((MapFragment) getFragmentManager()
		// .findFragmentById(R.id.map)).getMap();

		// LatLng sydney = new LatLng(-33.867, 151.206);

		// map.setMyLocationEnabled(true);
		// map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

		// map.addMarker(new MarkerOptions()
		// .title("Sydney")
		// .snippet("The most populous city in Australia.")
		// .position(sydney));
		double lat,lon,latitude = 0;
		double longitude = 0;
		addd(50.98138, 18.21434, "mojdom");
	addd(51.00274, 18.15491, "smardy");
		// create marker
    if (map != null) {
        map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }

map.setMyLocationEnabled(true);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
LocationListener loc_listener = new LocationListener() {

    public void onLocationChanged(Location l) {}

    public void onProviderEnabled(String p) {}

    public void onProviderDisabled(String p) {}

    public void onStatusChanged(String p, int status, Bundle extras) {}
};
//locationManager.requestLocationUpdates(bestProvider, 0, 0, loc_listener);
location = locationManager.getLastKnownLocation(bestProvider);
try {
    lat = location.getLatitude();
    lon = location.getLongitude();
} catch (NullPointerException e) {
    lat = -1.0;
    lon = -1.0;
}
Toast.makeText(this, "Myd"+lat+"-"+lon, Toast.LENGTH_LONG).show();
		/*
		 * 
		 * PolygonOptions rectOptions = new PolygonOptions() .add(new
		 * LatLng(37.35, -122.0), new LatLng(37.45, -122.0), new LatLng(37.45,
		 * -122.2), new LatLng(37.35, -122.2), new LatLng(37.35, -122.0));
		 */

		// Get back the mutable Polygon
		// Polygon polygon = map.addPolygon(rectOptions);

	}

	void addd(double a, double b, String c) {
		MarkerOptions marker = new MarkerOptions().position(new LatLng(a, b))
				.title(c);
		map.addMarker(marker);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void onClick_Przycisk_MAP_TYPE_SATELLITE(View v) {
		map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
		CameraUpdate up = CameraUpdateFactory.newLatLngZoom(LOCATION_BURNABY,
				16);
		map.animateCamera(up);
	}

	public void onClick_Przycisk_MAP_TYPE_TERRAIN(View v) {
		// map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
		// CameraUpdate
		// up=CameraUpdateFactory.newLatLngZoom(LOCATION_BURNABY,14);
		// map.animateCamera(up);
		SmsManager smsManager = SmsManager.getDefault();
		smsManager.sendTextMessage("500721601", null,
				"NIE ODPISUJ TYLKO DAJ ZNAC ZE DOSZEDL.MATEUSZ", null, null);
	}

	public boolean onMarkerClick(Marker arg0) {
		//if (arg0.getTitle().equals("mojdom")) {
			Button c = (Button) findViewById(R.id.button1);
			c.setText(arg0.getTitle());
		//}

		// if(arg0.getTitle().equals("Hello Maps ")) // if marker source is
		// clicked
		// Toast.makeText(MainActivity.this, arg0.getTitle(),2000).show();//
		// display toast
		return true;
		// return false;
	}

	


}
